package com.zhixue.content.controller;

import com.zhixue.content.feignclient.MediaServiceClient;
import com.zhixue.content.model.dto.*;
import com.zhixue.content.model.po.CourseBase;
import com.zhixue.content.service.CourseBaseService;
import com.zhixue.content.util.*;
import com.zhixue.model.*;
import com.zhixue.r.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Auther: 李 力
 * @Date: 2025/4/6
 * @Description: com.zhixue.content.controller
 * @version: 1.0
 */
@RestController
@RequestMapping
public class CourseBaseInfoController {

    @Autowired
    private MediaServiceClient mediaServiceClient;

    @Autowired
    private CourseBaseService baseService;

    //课程查询
    @PostMapping("/course/list")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    public PageResult<CourseBase> pageResult(PageParams pageParams, @RequestBody(required = false) QueryCourseParamsDto paramsDto) {
        CurrentUser user = SecurityUtil.getUser();
        String JGId = user.getCompanyId();
        return baseService.selectPageList(JGId, pageParams, paramsDto);
    }

    //新增课程
    @PostMapping("/course")
    public CourseBaseInfoDto addCourse(@RequestBody AddCourseDto courseDto) {
        //todo 单点登录的时候获取机构ID,目前写死
        return baseService.addCourse(courseDto, 1L);
    }

    //根据ID查询课程多方面的信息
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getInfoById(@PathVariable Long courseId) {
        return baseService.getInfoById(courseId);
    }

    //修改课程信息
    @PutMapping("/course")
    public CourseBaseInfoDto updateCourse(@RequestBody EditCourseDto editCourseDto) {
        //todo 单点登录的时候获取机构ID,目前写死
        return baseService.updateCourse(1L, editCourseDto);
    }

    //删除课程
    @DeleteMapping("/course/{id}")
    public R deleteById(@PathVariable("id") Long id) {
        return baseService.deleteById(id);
    }

    //    远程调用测试
    @PostMapping(value = "/upload/coursefile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

    public String upload(@RequestPart("filedata") MultipartFile filedata,
                         @RequestParam(value = "objectName", required = false) String objectName
    ) {
        mediaServiceClient.upload(filedata, objectName);
        return "OK";
    }
}
